document.addEventListener("load", function ()
{
	var trs = document.getElementsByClassName("bibItemsEntry"); //Chrome supports this method!(though non-standard)
	var tds_patroninfo_barcode = document.getElementsByClassName("patFuncBarcode"); //on /patroninfo*

	var Barcode_MouseOver = function ()
	{
		var span = document.createElement("span");
		document.tag_InfoSpan = span;
		
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function ()
		{
			if(xhr.readyState == 4 /*Request finished*/ && document.tag_InfoSpan)
			{
				var span = document.tag_InfoSpan;
				if(xhr.status == 200 /*OK*/)
				{
					var txt = xhr.responseText;
					var id = "var strWZxxxxxx = ";
					var location_il = txt.indexOf(id) + id.length + 1;
					var location = txt.substring(location_il, txt.indexOf("\"", location_il));

					if(location.length == 0)
					{
						span.innerText = chrome.i18n.getMessage("error_no_info");
						span.style.color = "#ff0000";
					}
					else
						span.innerText = location;
				}
				else
				{
					span.innerText = chrome.i18n.getMessage("error_retrieve_info");
					span.style.color = "#ff0000";
				}
			}
		}
		xhr.open("GET", "http://121.49.98.194:8080/dzkj/TSDW/GotoFlash.aspx?szBarCode=" + 
			this.innerText, true);
		xhr.send();
		
		span.style.position="absolute";
		span.style.left = event.clientX + document.body.scrollLeft + 20 + "px";
		span.style.top = event.clientY + document.body.scrollTop + 10 + "px";
		span.style.backgroundColor="#ffff80";
		span.style.fontWeight = "bold";
		span.style.color = "#3333ff";
		span.innerText = chrome.i18n.getMessage("status_retrieve");
		document.body.appendChild(span);
	}
	
	function MakeBarcodeLink(barcode_td)
	{
		if(barcode_td.children.length != 0)
			return;

		var barcode = barcode_td.innerText.replace("\u00a0", "").replace(" ", ""); //'\u00a0' is &nbsp;
		barcode_td.innerText = "";
		var a = document.createElement("a");
		a.innerText = barcode;
		a.href = "http://121.49.98.194:8080/dzkj/TSDW/GotoFlash.aspx?szBarCode=" + barcode;
		a.target = "_blank";
		a.onmouseover = Barcode_MouseOver;
		a.onmouseout = function ()
		{
			if(document.tag_InfoSpan)
			{
				document.body.removeChild(document.tag_InfoSpan);
				document.tag_InfoSpan = null;
			}
		}
		barcode_td.appendChild(a);
	}
	
	for(var i = 0; i < trs.length; ++i)
	{
		var barcode_td = trs[i].children.item(3);
		if(barcode_td)
			MakeBarcodeLink(barcode_td);
	}
	for(var i = 0; i < tds_patroninfo_barcode.length; ++i)
		MakeBarcodeLink(tds_patroninfo_barcode[i]);
}, true);
